package org.steven.crm.core.service.dict;

import org.steven.crm.core.query.DictQueryDataList;
import org.steven.crm.core.service.BaseService;
import com.steven.xdb.dbmanager.DBManager;
import com.steven.xdb.pagination.query.AbstractQueryDataList;
import com.steven.xdb.session.DBSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author wuzw
 * @Date 2021/5/24 21:30
 * @Version 1.0
 */
public class DictService extends BaseService {

    private static DictValueService dictValueService = new DictValueService();
    @Override
    protected String getTable() {
        return "dict";
    }

    @Override
    protected AbstractQueryDataList getQueryDataList() {
        return new DictQueryDataList();
    }

    @Override
    protected String getDataSourceName() {
        return null;
    }

    public Map<String, Object> readAll() throws Exception {
        Map<String, Object> result = new HashMap<>();
        DBSession session = getSession();

        Map<String, String> k = new HashMap<>();
        k.put("deleted", "0");

        List<Map<String, String>> dicts = DBManager.query(session, getTable(), "id, code", k);

        Map<String, List<Map<String, String>>> dictValueMap = dictValueService.readAllGroupByDictId();

        for (int i = 0 ; i < dicts.size(); ++i) {
            Map<String, String> dict = dicts.get(i);
            String id = dict.get("id");
            String code = dict.get("code");
            List<Map<String, String>> tmpDictValues = dictValueMap.get(id);
            if (null == tmpDictValues) {
                tmpDictValues = new ArrayList<>();
            }

            result.put(code, tmpDictValues);
        }

        return result;
    }

}
